package com.saicent.note.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.saicent.note.core.Article;
import com.saicent.note.core.Category;

@Controller
@RequestMapping("menu")
public class MenuController extends AbstractController {
	
	@RequestMapping
	public String allArticles(Model model) {
		List<Article> articles = getArticleManager().allArticles();
		model.addAttribute("articles", articles);
		resetCurrentCategory();
		return "menu";
	}
	
	@RequestMapping("{categoryId}")
	public String articlesOfCategory(
			Model model,
			@PathVariable("categoryId") String categoryId) throws Exception {
		Category category = getArticleManager().getCategory(categoryId);
		if (category == null) {
			return "redirect:/menu";
		}
		List<Article> articles = getArticleManager().getArticlesByCategory(categoryId);
		model.addAttribute("articles", articles);
		currentCategory(category);
		return "menu";
	}
}
